package com.lzh.dlykserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzh.dlykserver.constant.Constants;
import com.lzh.dlykserver.entity.TTranRemark;
import com.lzh.dlykserver.mapper.TTranRemarkMapper;
import com.lzh.dlykserver.query.TranQuery;
import com.lzh.dlykserver.query.TransactionQuery;
import com.lzh.dlykserver.service.TransactionRemarkService;
import com.lzh.dlykserver.util.JWTUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class TransactionRemarkServiceImpl implements TransactionRemarkService {

    private final TTranRemarkMapper tTranRemarkMapper;

    @Override
    public PageInfo<TTranRemark> getClueRemarkByPage(Integer current, TranQuery tranQuery) {
        PageHelper.startPage(current, Constants.PAGE_SIZE);

        List<TTranRemark> tClueRemarkList = tTranRemarkMapper.selectClueRemarkByPage(tranQuery);

        PageInfo<TTranRemark> info = new PageInfo<>(tClueRemarkList);

        return info;
    }

    @Override
    public TTranRemark getTranRemarkById(Integer id) {
        return tTranRemarkMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delClueMark(Integer id) {
        TTranRemark tTranRemark = new TTranRemark();
        tTranRemark.setId(id);
        tTranRemark.setDeleted(1);
        return tTranRemarkMapper.updateByPrimaryKeySelective(tTranRemark);
    }

    @Override
    public int updateTransactionRemark(TransactionQuery transactionQuery) {
        TTranRemark tTranRemark = new TTranRemark();
        BeanUtils.copyProperties(transactionQuery, tTranRemark);

        Integer id = JWTUtils.parseUserFromJWT(transactionQuery.getToken()).getId();
        tTranRemark.setEditBy(id);
        tTranRemark.setEditTime(new Date());

        return tTranRemarkMapper.updateByPrimaryKeySelective(tTranRemark);
    }


}
